package interpreter;

/**
 * @author Koizumi Sinya
 * @date 2018/03/19. 21:15
 * @edithor
 * @date
 */
public class Calculator {
    public int build(String statement) {
        Node left = null;
        Node right = null;
        Node last = null;

        String[] stateArr = statement.split(" ");

        for (int i = 0; i < stateArr.length; i++) {

            if ("*".equalsIgnoreCase(stateArr[i])) {
                //乘法
                left = last;
                int val = Integer.parseInt(stateArr[++i]);
                right = new ValueNode(val);
                last = new MulNode(left, right);


            } else if ("/".equalsIgnoreCase(stateArr[i])) {
                //除法
                left = last;
                int val = Integer.parseInt(stateArr[++i]);
                right = new ValueNode(val);
                last = new DivNode(left, right);

            } else {
                last = new ValueNode(Integer.parseInt(stateArr[i]));
            }
        }

        return last.interpret();
    }
}
